package word

import (
	"fmt"
	"github.com/yanyiwu/gojieba"
	"net/http"
	"strconv"
	"strings"
	"tesou.io/platform/poem-parent/poem-api/module/core/pojo"
	"tesou.io/platform/poem-parent/poem-api/module/core/vo"
	"tesou.io/platform/poem-parent/poem-core/module/core/service"
)

type FenciProcesser struct {
	FenciService service.FenciService
	ListService service.ListService
	SpiderUrl    string
	cookies      []*http.Cookie
}

func GetFenciProcesser() *FenciProcesser {
	return &FenciProcesser{}
}

//工作
func (this *FenciProcesser) Startup() {
	//1, 参数知道分词哪个类别，
	Type := 0

	//2，找出相关数据，（title,content)
	dataList := make([]vo.ListVO, 0)
	sql := "select * from t_list where SSynced = 0 AND Type >  " + strconv.Itoa(Type) + " AND Type !=26 limit 100"
	this.FenciService.Find(&dataList, sql)
	//base.Log.Info("Find:,", dataList)
	//3，分词
	var s string
	var words []string
	x := gojieba.NewJieba()
	defer x.Free()

	fenci_list_slice := make([]interface{}, 0)
	for _, v := range dataList {

		s = v.Title
		words = x.Extract(s, 5)
		fmt.Println("关键词抽取:", strings.Join(words, "/"))
		word_weights := x.ExtractWithWeight(s, 5)
		fmt.Println("关键词抽取:", word_weights)

		//4，保存分词结果
		for _, vv := range word_weights {
			//4.1保存
			chi := new(pojo.Fenci)
			chi.ObjectId =  v.Id
			chi.Word = vv.Word
			chi.Score = vv.Weight
			fenci_list_slice = append(fenci_list_slice, chi)

		}
		//4.2更新状态
		v.SSynced = 1
		this.ListService.Modify(&v)
	}
	fmt.Println("fenci_list_slice:", fenci_list_slice)
	this.FenciService.SaveList(fenci_list_slice)


}
